﻿/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace MicroFormatProcessing
{
    /// <summary>
    /// Collection of Session queries 
    /// </summary>   
    public class SessionRepository
    {
        private static CloudStorageAccount storageAccount;
        private SessionContext context;

        static SessionRepository()
        {
            CloudStorageAccount.SetConfigurationSettingPublisher(
                        (configName, configSetter) => configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)));
            storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(SessionContext.TABLE_NAME);
            // CloudTableClient.CreateTablesFromModel(typeof(MfUserTagContext), storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
        }

        public SessionRepository()
        {
            this.context = new SessionContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
            this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
            this.context.IgnoreResourceNotFoundException = true;
        }

        public IEnumerable<Session> SelectBySessionId(string mfSessionId)
        {
            return CreateQuery((from mfs in this.context.MfSessions
                                where mfs.PartitionKey == mfSessionId
                                select mfs).AsTableServiceQuery<Session>()).Execute();
        }

        public IEnumerable<Session> SelectByDayAndUser(string dayKey, string userId)
        {
            return CreateQuery((from mfs in this.context.MfSessions
                                where mfs.PartitionKey == dayKey && mfs.UserId == userId
                                select mfs).AsTableServiceQuery<Session>()).Execute();
        }

        public IEnumerable<Session> SelectByDayAndSessionId(string dayKey, string sessionId)
        {
            return CreateQuery((from mfs in this.context.MfSessions
                                where mfs.PartitionKey == dayKey && mfs.RowKey == sessionId
                                select mfs).AsTableServiceQuery<Session>()).Execute();
        }

        public void Add(Session mfSession)
        {
            this.context.Add(mfSession);
        }

        public void Update(Session mfSession)
        {
            this.context.Update(mfSession);
        }

        private CloudTableQuery<Session> CreateQuery(CloudTableQuery<Session> query)
        {
            if (this.context.MfSessions.FirstOrDefault() != null)
            {
                return query;
            }
            else
            {
                return (from mfs in this.context.MfSessions
                        select mfs).AsTableServiceQuery<Session>();
            }
        }
    }
}
